<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="/static/lib/layui-v2.5.6/layui-v2.5.6/layui/css/layui.css">
    <style>
        .layui-table-cell{
            height:36px;
            line-height: 36px;
        }
        .layui-col-xs1{
            height: 66px;
            line-height: 66px;
            margin: 5px 5px 0 0;
            text-align: center;
            font-size: 20px;
            cursor: pointer;
        }
        .info{
            font-size: 14px;
            color: rgb(110, 108, 108);
            line-height: 36px;
        }
        .layui-icon-search{
            font-size: 30px;
            color: #009688;
            cursor: pointer;
            margin-left: 40px;
        }
        .layui-icon{
            cursor: pointer;
        }
        
    </style>
</head>
<body>
    <table id="demo" lay-filter="test"></table>


<!-- 详细页面和弹出层 -->
    <div id="userDetails" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block userId_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户账号</label>
            <div class="layui-input-block userName_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block userNickname_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">付款ID</label>
            <div class="layui-input-block userPayId_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">预留手机</label>
            <div class="layui-input-block userPhone_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">创建时间</label>
              <div class="layui-input-inline userRegisterDate_info1 info"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">用户权限</label>
              <div class="layui-input-inline userRole_info1 info"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户状态</label>
            <div class="layui-input-block userStatus_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">更新时间</label>
            <div class="layui-input-block userUpdateDate_info1 info"></div>
        </div>
    </div>

    <div id="userEmpower" style="display: none; padding: 80px 20px;">
        <button type="button" class="layui-btn layui-btn-normal layui-btn-radius toAdmin">设为管理员</button>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-radius toSupplier">设为供应商</button>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-radius toUser">设为普通用户</button>
    </div>

    <div id="userdistributed" style="display: none;">
        <table id="demo1" lay-filter="machinetest"></table>
    </div>


    <script type="text/html" id="barDemo">
        <i class="layui-icon layui-icon-tabs" lay-event="userDetails_1" title="用户详情"></i>
        <i class="layui-icon layui-icon-key" lay-event="userEmpower_1" title="用户赋权"></i>
    </script>

    <script type="text/html" id="barDemo1">
        <i class="layui-icon layui-icon-ok" lay-event="userdistributed_1_btn" title="分配"></i>
    </script>

    <script type="text/html" id="toolbarDemo">
        
        <div class="layui-inline" style="width: 150px;">
            <select name="role_2" lay-verify="" id="role_2">
                <option value="">用户类型</option>
                <option value="admin">管理员</option>
                <option value="supplier">合作供应商</option>
                <option value="user">普通用户</option>
            </select>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 280px;">
              <input type="text" name="name_2" id="name_2" placeholder="输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 150px;">
              <input type="text" name="nickname_2" id="nickname_2" placeholder="输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 150px;">
              <input type="text" name="phone_2" id="phone_2" placeholder="输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 120px;">
            <input type="text" name="start_2" id="start_2" placeholder="起始时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        -
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 120px;">
                <input type="text" name="end_2" id="end_2" placeholder="结束时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <i class="layui-icon layui-icon-search" lay-event="getUserListSearch_2" title="搜索刷新"></i>
        </div>  
    </script>
<!-- 结束 -->

    
   
    <script src="/static/lib/layui-v2.5.6/layui-v2.5.6/layui/layui.js"></script>
    <script>
        
        
        
        

        layui.use(['table','laydate','form','util','layer'], function(){
            var $ = layui.jquery;
            var table = layui.table;
            var laydate =layui.laydate;
            var util = layui.util;
            var layer = layui.layer;

            table.render({
                elem: '#demo',
                height: 'full-60',
                method: 'post',
                                //TODO:
                url: '/admin/user/queryUser', //数据接口
                // page: true, //开启分页
                toolbar: '#toolbarDemo',
                defaultToolbar: [],
                id: 'crmTest',
                cols: [[ //表头
                {type:'numbers'},
                {field: 'id', title: '用户ID', width:'20%'},
                {field: 'nickname', title: '用户名称'} ,
                {field: 'registerDate', title: '创建时间', templet:"<div>{{layui.util.toDateString(Number(d.registerDate), 'yyyy.MM.dd HH:mm:ss')}}</div>", width:'15%', sort: true},
                {field: 'phone', title: '手机号', templet:'<div>{{phonefun(d.phone)}}</div>', width: '15%'},
                {field: 'status', title: '用户状态', width: '7%'},
                {field: 'role', title: '用户权限', width: '10%', templet:'<div>{{rolefun(d.role)}}</div>'},
                {fixed: 'right', title: '操作', width:'10%', align:'center', toolbar: '#barDemo'}
                ]],


                parseData: function(res){ //res 即为原始返回的数据
                    return {
                    "code": res.success, //解析接口状态
                    "msg": res.message, //解析提示文本
                    // "count": res.data.total, //解析数据长度
                    "data": res.data //解析数据列表
                    };
                },
                request: {
                    pageName: 'page', //页码的参数名称，默认：page
                    // limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                response: {
                    // statusName: 'success', //规定数据状态的字段名称，默认：code
                    statusCode: true //规定成功的状态码，默认：0
                }
            });
            laydate.render({
                elem: '#start_2',
                format: 'yyyy/MM/dd',
            });
            laydate.render({
                elem: '#end_2',
                format: 'yyyy/MM/dd'
            });
        
            table.on('toolbar(test)', function(obj){
                sessionStorage.setItem("crmRole_2_1", $("#role_2").val());
                sessionStorage.setItem("crmName_2_1", $("#name_2").val());
                sessionStorage.setItem("crmNickname_2_1", $("#nickname_2").val());
                sessionStorage.setItem("crmPhone_2_1", $("#phone_2").val());
                sessionStorage.setItem("crmStart_2_1", $("#start_2").val());
                sessionStorage.setItem("crmEnd_2_1", $("#end_2").val());
                //搜索栏的实现
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getUserListSearch_2':
                    
                    
                    table.reload('crmTest', {
                        where: { //设定异步数据接口的额外参数，任意设
                            role: $("#role_2 option:selected").attr('value') || undefined,
                            nickname: $("#nickname_2").val() || undefined,
                            phone:$("#phone_2").val() || undefined,
                            name: $("#name_2").val() || undefined,
                            registerStartDate: dateTime($("#start_2").val()) || undefined,
                            registerEndDate: dateTime($("#end_2").val()) || undefined,
                        },
                        done: function(res, curr, count){
                            this.where={};
                        }
                    });
                    
                    laydate.render({
                        elem: '#start_2',
                        format: 'yyyy/MM/dd',
                    });
                    laydate.render({
                        elem: '#end_2',
                        format: 'yyyy/MM/dd'
                    });

                    if (sessionStorage.getItem("crmRole_2_1") == '') {
                        $("#role_2").attr('text','用户类型')
                    }else if(sessionStorage.getItem("crmRole_2_1") == 'admin'){
                        $("#role_2 option[value= 'admin']").attr("selected", true);
                    }else if (sessionStorage.getItem("crmRole_2_1") == 'supplier') {
                        $("#role_2 option[value= 'supplier']").attr("selected", true);
                    }else if (sessionStorage.getItem("crmRole_2_1") == 'user') {
                        $("#role_2 option[value= 'user']").attr("selected", true);
                    }

                    

                    if (sessionStorage.getItem("crmName_2_1") == '') {
                        $("#name_2").attr('placeholder','输入账号')
                    }else{
                        $("#name_2").attr('placeholder',sessionStorage.getItem("crmName_2_1"))
                    }

                    if (sessionStorage.getItem("crmNickname_2_1") == '') {
                        $("#nickname_2").attr('placeholder','输入用户名')
                    }else{
                        $("#nickname_2").attr('placeholder',sessionStorage.getItem("crmNickname_2_1"))
                    }

                    if (sessionStorage.getItem("crmPhone_2_1") == '') {
                        $("#phone_2").attr('placeholder','输入手机号')
                    }else{
                        $("#phone_2").attr('placeholder',sessionStorage.getItem("crmPhone_2_1"))
                    }
                    
                    if (sessionStorage.getItem("crmStart_2_1") == '') {
                        $("#start_2").attr('placeholder','起始时间')
                    }else{
                        $("#start_2").attr('value',sessionStorage.getItem("crmStart_2_1"))
                    }
                    if (sessionStorage.getItem("crmEnd_2_1") == '') {
                        $("#end_2").attr('placeholder','结束时间')
                    }else{
                        $("#end_2").attr('value',sessionStorage.getItem("crmEnd_2_1"))
                    }
                    
                    break;
                };
            });

            table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
                if(layEvent === 'userDetails_1'){
                    $(".userId_info1").html(data.id)
                    $(".userName_info1").html(data.name)
                    $(".userNickname_info1").html(data.nickname)
                    $(".userPayId_info1").html(data.payId)
                    $(".userPhone_info1").html(data.phone)
                    $(".userRegisterDate_info1").html(ctime(data.registerDate))
                    $(".userRole_info1").html(rolefun(data.role))
                    $(".userStatus_info1").html(data.status)
                    $(".userUpdateDate_info1").html(data.updateDate)
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['66%', '90%'], //宽高
                        title: '用户详细',
                        btn: ['关闭'],
                        content: $('#userDetails'),
                        
                    });
                }else if (layEvent === 'userEmpower_1') {
                    var index = layer.open({
                        type: 1,
                        area: ['400px', '300px'], //宽高
                        title: '设置用户权限',
                        btn: ['取消'],
                        content: $('#userEmpower'),
                    });
                    $(".toAdmin").click(function(){
                        layer.confirm('确定更改赋权？', function(index2){
                            $.ajax({
                                type: "post",
                                        //TODO:
                                url: "/admin/user/empower",
                                data: {user_id: data.id,role: 'admin'},
                                dataType: "json",
                                success: function(mag){
                                    if (mag.success == true) {
                                        layer.msg(mag.message)
                                        table.reload('crmTest')
                                        layer.close(index);
                                    }else{
                                        layer.msg(mag.message)
                                    }
                                    laydate.render({
                                        elem: '#start_2',
                                        format: 'yyyy/MM/dd',
                                    });
                                    laydate.render({
                                        elem: '#end_2',
                                        format: 'yyyy/MM/dd'
                                    })
                                },
                                error:function(){
                                    layer.msg('操作失败,请重新登录操作')
                                }
                            })
                        })
                    })
                    $(".toSupplier").click(function(){
                        layer.confirm('确定更改赋权？', function(index2){
                            $.ajax({
                                type: "post",
                                        //TODO:
                                url: "/admin/user/empower",
                                data: {user_id: data.id,role: 'supplier'},
                                dataType: "json",
                                success: function(mag){
                                    if (mag.success == true) {
                                        layer.msg(mag.message)
                                        table.reload('crmTest')
                                        layer.close(index);
                                        laydate.render({
                                            elem: '#start_2',
                                            format: 'yyyy/MM/dd',
                                        });
                                        laydate.render({
                                            elem: '#end_2',
                                            format: 'yyyy/MM/dd'
                                        })
                                    }else{
                                        layer.msg(mag.message)
                                    }
                                },
                                error:function(){
                                    layer.msg('操作失败,请重新登录操作')
                                }
                            })
                        })
                    })
                    $(".toUser").click(function(){
                        layer.confirm('确定更改赋权？', function(index2){
                            $.ajax({
                                type: "post",
                                        //TODO:
                                url: "/admin/user/empower",
                                data: {user_id: data.id,role: 'user'},
                                dataType: "json",
                                success: function(mag){
                                    if (mag.success == true) {
                                        layer.msg(mag.message)
                                        table.reload('crmTest')
                                        laydate.render({
                                            elem: '#start_2',
                                            format: 'yyyy/MM/dd',
                                        });
                                        laydate.render({
                                            elem: '#end_2',
                                            format: 'yyyy/MM/dd'
                                        })
                                        layer.close(index);
                                    }else{
                                        layer.msg(mag.message)
                                    }
                                },
                                error:function(){
                                    layer.msg('操作失败,请重新登录操作')
                                }
                            })
                        })
                    })
                }
            });
        });

        function dateTime(date) {
            if (date != '') {
                var dateTime = new Date(date).getTime()
            }else{
                var dateTime = ''
            }
            return dateTime
        }
        function ctime(num) {
            if (num == null) {
                var ctime = '订单未完成'
            }else {
                var ctime = layui.util.toDateString(Number(num), "yyyy.MM.dd HH:mm:ss")
            }
            return ctime
        }
        function phonefun(num) {
            if(num == null || num == undefined ||num == '') {
                var phone = '<span style="color:#f173e1;">未填写手机号</span>'
            }else {
                var phone = num
            }
            return phone
        }
        function rolefun(num) {
            if (num == 'user') {
                var role = '普通用户'
            }else if (num == 'admin') {
                var role = '<span style="color:#f173e1;">管理员</span>'
            }else if (num == 'supplier') {
                var role = '<span style="color:#f173e1;">合作供应商</span>'
            }else{
                var role = '权限状态未知'
            }
            return role
        }
        function paymethodfun(num) {
            if (num == '0') {
                var paymethod = '支付宝'
            }else if (num == '1'){
                var paymethod = '微信'
            }else if (num == '2'){
                var paymethod = '账户余额'
            }else if (num == null){
                var paymethod = '订单未完成'
            }else {
                var paymethod = '其他支付方式'
            }
            return paymethod
        }
        function stockStatus(num) {
            if (num == 0) {
                var stockStatus = '正常'
            }else {
                var stockStatus = '<span style="color:#f173e1;">缺货</span>'
            }
            return stockStatus
        }
        function machineStatus(num) {
            if (num == 0) {
                var machineStatus = '正常'
            }else {
                var machineStatus = '<span style="color:#f173e1;">未上架</span>'
            }
            return machineStatus
        }
        function typefun(num) {
            if (num == 0) {
                var type = '售货机'
            }else {
                var type = '代收门店'
            }
            return type
        }
    </script>
</body>
</html>